#include "Font/FontService.h"
#include "Font/Font.h"

#include "Core/Kernel.h"
#include "Image/Image.h"
#include "Image/ImageService.h"

#include <ft2build.h>
#include FT_FREETYPE_H

namespace Orca {

	//--------------------------------------------------------------------------
	ORCA_IMPLEMENT_SERVICE(FontService)

	//--------------------------------------------------------------------------
	FontService::FontService() {

	}

	//--------------------------------------------------------------------------
	FontService::~FontService() {

	}

	//--------------------------------------------------------------------------
	bool FontService::startup() {

		return true;
	}

	//--------------------------------------------------------------------------
	void FontService::step(float dt) {

	}

	//--------------------------------------------------------------------------
	void FontService::shutdown() {

	}

	//--------------------------------------------------------------------------
	Font* FontService::createFont(const Str& filename, uint32 size) {

		FT_Library library;
		FT_Face face;
		
		FT_Error error = FT_Init_FreeType( &library );
		
		if( error ) {
		

		}
		
		// load font-face
		error = FT_New_Face( library, filename.c_str(), 0, &face );
		
		if( error == FT_Err_Unknown_File_Format ) {
		}
		else if( error ) {
		}

		// set font size
		error = FT_Set_Pixel_Sizes( face, 0, 16 );

		Image *img = INST<ImageService>()->createImage("RGBA8", 256, 256);

		FT_Int32 load_flags = FT_LOAD_DEFAULT;
		FT_Render_Mode render_mode = FT_RENDER_MODE_NORMAL;	// use 256 gray scale render mode

		for(FT_ULong charcode = 0; charcode < 256; ++charcode) {

			FT_UInt glyph_index = FT_Get_Char_Index( face, charcode );

			error = FT_Load_Glyph( face, glyph_index, load_flags );

			error = FT_Render_Glyph( face->glyph, render_mode );

			// we are now ready to render the char to a bitmap

		}

		delete img;

		FT_Done_Face(face);
		FT_Done_FreeType(library);

		Font *font = new Font;

		return font;
	}

	//--------------------------------------------------------------------------
}